package com.hjm.project.job.task.service.impl;


import java.io.File;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.poi.ExcelUtil;

import com.hjm.framework.config.BlueConfig;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.hjm.project.job.task.mapper.JobTaskMapper;
import com.hjm.project.job.task.domain.JobTask;
import com.hjm.project.job.task.service.IJobTaskService;
import com.hjm.common.utils.text.Convert;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件下载队列Service业务层处理
 * 
 * @author maker-platform
 * @date 2021-01-16
 */
@Service
public class JobTaskServiceImpl implements IJobTaskService 
{
    @Autowired
    private JobTaskMapper jobTaskMapper;

    /**
     * 查询文件下载队列
     * 
     * @param id 文件下载队列ID
     * @return 文件下载队列
     */
    @Override
    public JobTask selectJobTaskById(Long id)
    {
        return jobTaskMapper.selectJobTaskById(id);
    }

    /**
     * 查询文件下载队列列表
     * 
     * @param jobTask 文件下载队列
     * @return 文件下载队列
     */
    @Override
    public List<JobTask> selectJobTaskList(JobTask jobTask)
    {
        return jobTaskMapper.selectJobTaskList(jobTask);
    }

    /**
     * 查询文件下载队列列表数据管理
     *
     * @param jobTask 文件下载队列
     * @return 文件下载队列
     */
    @Override
    public List<JobTask> selectJobTaskListOfData(JobTask jobTask)
    {
        return jobTaskMapper.selectJobTaskListOfData(jobTask);
    }

    /**
     * 获取所有文件下载队列列表
     *
     * @param jobTask 文件下载队列
     * @return 文件下载队列
     */
    @Override
    public List<JobTask> selectAllJobTaskList(JobTask jobTask)
    {
        return jobTaskMapper.selectAllJobTaskList(jobTask);
    }

    /**
     * 新增文件下载队列
     * 
     * @param jobTask 文件下载队列
     * @return 结果
     */
    @Override
    public int insertJobTask(JobTask jobTask)
    {
        jobTask.setCreateTime(DateUtils.getNowDate());
        return jobTaskMapper.insertJobTask(jobTask);
    }

    /**
     * 修改文件下载队列
     * 
     * @param jobTask 文件下载队列
     * @return 结果
     */
    @Override
    public int updateJobTask(JobTask jobTask)
    {
        jobTask.setUpdateTime(DateUtils.getNowDate());
        return jobTaskMapper.updateJobTask(jobTask);
    }

    /**
     * 删除文件下载队列对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobTaskByIds(String ids)
    {
        return jobTaskMapper.deleteJobTaskByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除文件下载队列信息
     * 
     * @param id 文件下载队列ID
     * @return 结果
     */
    @Override
    public int deleteJobTaskById(Long id)
    {
        return jobTaskMapper.deleteJobTaskById(id);
    }

    @Override
    @Async
    public void asyncDownLoad(String sheetName,Long operatorType,String operatorContent,String createBy,List list,ExcelUtil util) {
        try {
            if ( sheetName==null || operatorContent==null || createBy==null || list == null ||list.size()==0 ||util==null ){
                return;
            }
            JobTask jobTask=new JobTask();
            String fileName=DateUtils.getSimpleDateTimeMillisecond() + "_" + sheetName + ".xlsx";
            jobTask.setOperatorType(operatorType);
            jobTask.setFileName(fileName);
            jobTask.setOperatorContent(operatorContent);
            jobTask.setSource(Long.parseLong("0"));
            jobTask.setCreateBy(createBy);
            jobTask.setCreateTime(DateUtils.getNowDate());
            jobTask.setStatus(Long.parseLong("2"));
            jobTask.setIsDelete(Long.parseLong("0"));
            jobTaskMapper.insertJobTask(jobTask);
            int result =util.exportExcel(list, sheetName,fileName);
            if (result>0){
                jobTask.setUpdateBy(createBy);
                jobTask.setUpdateTime(DateUtils.getNowDate());
                jobTask.setStatus(Long.parseLong("0"));
            }else{
                jobTask.setUpdateBy(createBy);
                jobTask.setUpdateTime(DateUtils.getNowDate());
                jobTask.setStatus(Long.parseLong("1"));
            }
            jobTaskMapper.updateJobTask(jobTask);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 导入的文件增加一条导入记录 可在队列下载当前的记录
     * @Param [sheetName, operatorType, operatorContent, createBy, list, util]
     * @Return void
     */
    @Async
    public void asyncUploadFile(String operatorType, String operatorContent, String createBy, MultipartFile file) {
        try {

            //获取一个要上传的路径
            String downloadPath = BlueConfig.getDownloadPath();
            //创建文件目录
            File files = new File(downloadPath);
            if (!files.exists()) {
                files.mkdirs();
            }

            //上传的文件名称
            String fileName = DateUtils.dateTimeNow() + "_" + file.getOriginalFilename();

            //文件是否存在，不存在创建
            File fileRead = new File(downloadPath, fileName);
            if (!fileRead.exists()) {
                files.createNewFile();
            }
            //file.transferTo(fileRead);
            FileUtils.copyInputStreamToFile(file.getInputStream(),fileRead);
            JobTask jobTask=new JobTask();
            jobTask.setOperatorType(Long.parseLong(operatorType));
            jobTask.setFileName(fileName);
            jobTask.setOperatorContent(operatorContent);
            jobTask.setSource(Long.parseLong("0"));
            jobTask.setCreateBy(createBy);
            jobTask.setCreateTime(DateUtils.getNowDate());
            jobTask.setStatus(Long.parseLong("2"));
            jobTask.setIsDelete(Long.parseLong("0"));
            int i = jobTaskMapper.insertJobTask(jobTask);
            if (i > 0 ){
                jobTask.setUpdateBy(createBy);
                jobTask.setUpdateTime(DateUtils.getNowDate());
                jobTask.setStatus(Long.parseLong("0"));
            }else{
                jobTask.setUpdateBy(createBy);
                jobTask.setUpdateTime(DateUtils.getNowDate());
                jobTask.setStatus(Long.parseLong("1"));
            }
            jobTaskMapper.updateJobTask(jobTask);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    /**
     * 导入的文件增加一条导入记录 可在队列下载当前的记录
     * @Param [sheetName, operatorType, operatorContent, createBy, list, util]
     * @Return void
     */
    @Async
    public void asyncUploadFileFuture(CompletableFuture<JobTask> cbf) throws Exception {
        CompletableFuture.allOf(cbf).join();
        JobTask jobTasks = cbf.get();
        try {
            //获取一个要上传的路径
            String downloadPath = BlueConfig.getDownloadPath();
            //创建文件目录
            File files = new File(downloadPath);
            if (!files.exists()) {
                files.mkdirs();
            }
            JobTask jobTask=new JobTask();
            jobTask.setOperatorType(jobTasks.getOperatorType());
            jobTask.setFileName(jobTasks.getFileName());
            jobTask.setOperatorContent(jobTasks.getOperatorContent());
            jobTask.setSource(Long.parseLong("0"));
            jobTask.setCreateBy(jobTasks.getCreateBy());
            jobTask.setCreateTime(DateUtils.getNowDate());
            jobTask.setStatus(Long.parseLong("2"));
            jobTask.setIsDelete(Long.parseLong("0"));
            int i = jobTaskMapper.insertJobTask(jobTask);
            if (i > 0 ){
                jobTask.setUpdateBy(jobTasks.getCreateBy());
                jobTask.setUpdateTime(DateUtils.getNowDate());
                jobTask.setStatus(Long.parseLong("0"));
            }else{
                jobTask.setUpdateBy(jobTasks.getCreateBy());
                jobTask.setUpdateTime(DateUtils.getNowDate());
                jobTask.setStatus(Long.parseLong("1"));
            }
            jobTaskMapper.updateJobTask(jobTask);
        }catch (Exception e){
            e.printStackTrace();
        }

    }



}
